<?php

namespace app\admin\model;

use app\admin\common\Base;
use app\admin\validate\SeoLink;
use app\admin\validate\Products as ProductsValidate;
use think\Model;
class Products extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = 'qa_product';

    protected $insert = ['status','is_recommended','create_time','update_time'];
    protected $update = ['status','is_recommended','update_time'];

    public function add($data){
        //实例化验证器
        $validate = new ProductsValidate();
        //验证器验证
        if (!$validate->check($data)){
            return ['code'=>'err','msg'=>$validate->getError()];
        }
        //处理数据
        $data = $this->handleData($data,'add');
        if ($data['code'] == 'suc'){
            //写入数据库记录
            $result = self::allowField(true)->create($data['data']);
            if ($result){
                insert_system_log('新增产品：' . $result);
                return ['code' => 'suc','msg'=>'新增成功'];
            }else{
                return ['code' => 'err','msg' => '添加产品失败，请重试'];
            }
        }
        return ['code'=>$data['code'],'msg'=>$data['msg']];
    }

    public function edit($id,$data){
        //实例化验证器
        $validate = new ProductsValidate();
        //验证器验证
        if (!$validate->check($data)){
            return ['code'=>'err','msg'=>$validate->getError()];
        }
        //处理数据
        $data = $this->handleData($data,'edit',$id);
        if ($data['code'] == 'suc') {
            //更新数据库记录
            $result = self::save($data['data'], ['id' => $id]);
            if ($result) {
                insert_system_log('编辑产品：' . $id);
                return ['code' => 'suc', 'msg' => '编辑成功'];
            } else {
                return ['code' => 'err', 'msg' => '编辑产品失败，请重试'];
            }
        }
        return ['code'=>$data['code'],'msg'=>$data['msg']];
    }

    private function handleData($data,$action,$id=''){

        if (!empty($data['seo_link'])) {
            //实例化自定义链接验证器
            $link_validate = new SeoLink();
            //实例化基类控制器
            $base_controller = new Base();
            if (!$link_validate->check($data)) {
                //链接格式错误
                return ['code' => 'err', 'msg' => $link_validate->getError()];
            }
            if ($action == 'add') {
                //新增操作
                $data['seo_link'] = $base_controller->handleSeoUrl($data['seo_link'], 'product', 'add');
            } else {
                //修改操作
                $data['seo_link'] = $base_controller->handleSeoUrl($data['seo_link'], 'product', 'edit', $id);
            }
        } else {
            $data['seo_link'] = null;
        }

        $carousel_arr = [];
        if (!empty($data['carousel'])){
            $data['carousel'] = explode(',',$data['carousel']);
            foreach ($data['carousel'] as $k => $v){
                $temp = [];
                $temp['path'] = $v;
                $temp['alt'] = $data['carousel_keywords'][$k];
                array_push($carousel_arr,$temp);
            }
        }
        unset($data['carousel_keywords']);
        $data['carousel'] = json_encode($carousel_arr);

        /*if (!empty($data['related_products'])) {
            $data['related_products'] = implode(',', json_decode($data['related_products']));
        } else {
            $data['related_products'] = null;
        }*/

        return ['code' => 'suc', 'data' => $data];
    }

    protected function setStatusAttr($value){
        return empty($value) ? 0 : 1;
    }

    protected function setIsRecommendedAttr($value){
        return empty($value) ? 0 : 1;
    }

    protected function setCreateTimeAttr(){
        return time();
    }

    protected function setUpdateTimeAttr(){
        return time();
    }
}